package com.qianying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianying.comment.CommonResult;
import com.qianying.entity.OrderStatus;
import com.qianying.mapper.OrderStatusMapper;
import com.qianying.service.OrderStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * 订单状态Service实现类
 */
@Service
public class OrderStatusServiceImpl extends ServiceImpl<OrderStatusMapper, OrderStatus> implements OrderStatusService {

    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Override
    public CommonResult<IPage<OrderStatus>> getPage(Page<OrderStatus> page, String statusName) {
        LambdaQueryWrapper<OrderStatus> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(statusName)) {
            queryWrapper.like(OrderStatus::getStatusName, statusName);
        }
        // 按ID升序排列
        queryWrapper.orderByAsc(OrderStatus::getId);
        IPage<OrderStatus> resultPage = orderStatusMapper.selectPage(page, queryWrapper);
        return CommonResult.success(resultPage);
    }

    @Override
    public CommonResult<OrderStatus> getById(Long id) {
        if (id == null) {
            return CommonResult.failed("ID不能为空");
        }
        OrderStatus orderStatus = orderStatusMapper.selectById(id);
        if (orderStatus == null) {
            return CommonResult.failed("未找到ID为" + id + "的订单状态");
        }
        return CommonResult.success(orderStatus);
    }

    @Override
    public CommonResult<?> saveStatus(OrderStatus orderStatus) {
        // 校验必填项
        if (!StringUtils.hasText(orderStatus.getStatusName())) {
            return CommonResult.failed("订单状态名称不能为空");
        }
        if (!StringUtils.hasText(orderStatus.getStatusCode())) {
            return CommonResult.failed("订单状态代号不能为空");
        }

        // 校验名称唯一性
        OrderStatus nameExist = orderStatusMapper.selectByName(orderStatus.getStatusName());
        if (nameExist != null) {
            return CommonResult.failed("订单状态名称已存在");
        }

        // 校验代号唯一性
        OrderStatus codeExist = orderStatusMapper.selectByCode(orderStatus.getStatusCode());
        if (codeExist != null) {
            return CommonResult.failed("订单状态代号已存在");
        }
        orderStatus.setId(null);
        int rows = orderStatusMapper.insert(orderStatus);
        return rows > 0 ? CommonResult.success("新增订单状态成功") : CommonResult.failed("新增订单状态失败");
    }

    @Override
    public CommonResult<?> updateStatus(OrderStatus orderStatus) {
        // 校验必填项
        if (orderStatus.getId() == null) {
            return CommonResult.failed("ID不能为空");
        }
        if (!StringUtils.hasText(orderStatus.getStatusName())) {
            return CommonResult.failed("订单状态名称不能为空");
        }
        if (!StringUtils.hasText(orderStatus.getStatusCode())) {
            return CommonResult.failed("订单状态代号不能为空");
        }

        // 检查记录是否存在
        OrderStatus existing = orderStatusMapper.selectById(orderStatus.getId());
        if (existing == null) {
            return CommonResult.failed("未找到ID为" + orderStatus.getId() + "的订单状态");
        }

        // 校验名称唯一性（排除当前记录）
        OrderStatus nameExist = orderStatusMapper.selectByName(orderStatus.getStatusName());
        if (nameExist != null && !nameExist.getId().equals(orderStatus.getId())) {
            return CommonResult.failed("订单状态名称已存在");
        }

        // 校验代号唯一性（排除当前记录）
        OrderStatus codeExist = orderStatusMapper.selectByCode(orderStatus.getStatusCode());
        if (codeExist != null && !codeExist.getId().equals(orderStatus.getId())) {
            return CommonResult.failed("订单状态代号已存在");
        }

        int rows = orderStatusMapper.updateById(orderStatus);
        return rows > 0 ? CommonResult.success("更新订单状态成功") : CommonResult.failed("更新订单状态失败");
    }

    @Override
    public CommonResult<?> removeById(Long id) {
        if (id == null) {
            return CommonResult.failed("ID不能为空");
        }
        // 检查记录是否存在
        OrderStatus existing = orderStatusMapper.selectById(id);
        if (existing == null) {
            return CommonResult.failed("未找到ID为" + id + "的订单状态");
        }
        int rows = orderStatusMapper.deleteById(id);
        return rows > 0 ? CommonResult.success("删除订单状态成功") : CommonResult.failed("删除订单状态失败");
    }

    @Override
    public CommonResult<List<OrderStatus>> getAll() {
        // 查询所有订单状态并按ID排序
        LambdaQueryWrapper<OrderStatus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(OrderStatus::getId);
        List<OrderStatus> orderStatusList = orderStatusMapper.selectList(queryWrapper);
        return CommonResult.success(orderStatusList);
    }
}
